package com.huawei.Galenus.controller;

import com.alibaba.fastjson.JSONObject;
import com.huawei.Galenus.domain.Message;
import com.huawei.Galenus.domain.Singer;
import com.huawei.Galenus.domain.User;
import com.huawei.Galenus.service.FabricService;
import com.huawei.Galenus.service.SingerService;
import com.huawei.Galenus.service.UserService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
@Api("用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FabricService fabricService;

    @Autowired
    private SingerService singerService;

    @ApiOperation(value = "用户注册", httpMethod = "POST")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Message register(@ApiParam("用户名") @RequestParam("username")String username,
                            @ApiParam("密码")@RequestParam("password")String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        if (user.getUsername() == "" || user.getUsername() == null
                || user.getPassword() == "" || user.getPassword() == null)
            return new Message().error(1111, "用户名密码不能为空");
        if(userService.findByUsername(user.getUsername()) != null)
            return new Message().error(1111, "用户已存在");
        String id = UUID.randomUUID().toString();
        int type = 0;
        int memberlevel = 0;
        int enjoyvalue = 0;
        String method = "adduser";
        String[] params = new String[]{id};
        JSONObject payload = fabricService.songInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            user.setId(id);
            user.setEnjoyvalue(enjoyvalue);
            user.setMemberlevel(memberlevel);
            user.setType(type);
            userService.save(user);
            return new Message().ok(6666, "注册成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "注册失败").addData("payload", payload);
        }
    }

    @ApiOperation(value = "登录", httpMethod = "POST")
    @PostMapping("/login")
    public Message login(@ApiParam("用户名") @RequestParam("username")String username,
                         @ApiParam("密码")@RequestParam("password")String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        if (user.getUsername() == "" || user.getUsername() == null
                || user.getPassword() == "" || user.getPassword() == null)
            return new Message().error(1111, "用户名密码不能为空");
        if(userService.findByUsername(user.getUsername()) == null
                || !user.getPassword().equals(userService.findByUsername(user.getUsername()).getPassword())){
            return new Message().error(1111, "用户不存在或者密码错误");
        }
        String jwtToken = Jwts.builder().setSubject(user.getUsername()).claim("roles", "member").setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, "secretkey").compact();
        return new Message().ok(6666, "登录成功").addData("token", jwtToken)
                .addData("uid", userService.findByUsername(user.getUsername()).getId());
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/quinfo")
    public Message quinfo(@RequestParam("uid") String uid) {
        String[] params = new String[]{uid};
        String method = "quinfo";
        JSONObject payload = fabricService.songQuery(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "获取用户信息成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "获取用户信息失败").addData("payload", payload);
        }
    }

    @ApiOperation("购买会员")
    @PutMapping("/fbmem")
    public Message fbmem(@RequestParam("uid") String uid) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", uid);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "fbmem";
        JSONObject payload = fabricService.songInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            userService.beMember(uid);
            return new Message().ok(6666, "购买会员成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "购买会员失败").addData("payload", payload);
        }
    }

    @ApiOperation("充值欢乐值")
    @PutMapping("/evalue")
    public Message evalue(@RequestParam("uid") String uid, @RequestParam("amount") int amount) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", uid);
        String[] params = new String[]{jsonObject.toJSONString(), amount + ""};
        String method = "evalue";
        JSONObject payload = fabricService.songInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            userService.enjoy(uid, amount);
            return new Message().ok(6666, "充值成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "充值失败").addData("payload", payload);
        }
    }

    @ApiOperation("艺人认证")
    @PostMapping("/singer")
    public Message authSinger(@RequestParam("id") String uid, @RequestParam("stageName") String stageName,
                              @RequestParam("company") String company, @RequestParam("realName") String realName,
                              @RequestParam("idCard") String idCard) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", uid);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "utype";
        JSONObject payload = fabricService.songInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            userService.changeType(uid);
            Singer singer = new Singer();
            singer.setId(uid);
            singer.setCompany(company);
            singer.setStageName(stageName);
            singer.setRealName(realName);
            singer.setIdCard(idCard);
            singerService.save(singer);
            return new Message().ok(6666, "认证成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "认证失败").addData("payload", payload);
        }
    }

    @ApiOperation("查看歌手列表")
    @GetMapping("/singers")
    public Message getSingers() {
        List<User> singers = userService.getSingers();
        return new Message().ok(6666, "获取成功").addData("singers", singers);
    }

    @ApiOperation("查看我的票数")
    @GetMapping("/my_tickets")
    public Message getMyTickets(@RequestParam("uid") String uid) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", uid);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "member.getOneVotes";
        JSONObject payload = fabricService.ssaQuery(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "查看成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "查看成功").addData("payload", null);
        }
    }

}

